package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TPAFDdata;
import com.wit.witmg.entity.TPAPEdata;
import com.wit.witmg.entity.TPAdata;
import com.wit.witmg.mapper.TPAdataMapper;
import com.wit.witmg.service.ITLogService;
import com.wit.witmg.service.ITPAFDdataService;
import com.wit.witmg.service.ITPAPEdataService;
import com.wit.witmg.service.ITPAdataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.JsonResult;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.awt.geom.QuadCurve2D;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 粒度分析主表 服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-04-28
 */
@Service
@RequiredArgsConstructor
public class TPAdataServiceImpl extends ServiceImpl<TPAdataMapper, TPAdata> implements ITPAdataService {
    final ITPAFDdataService  tpaFDdataService;
    final ITPAPEdataService tpaPEdataService;
    final ITLogService logService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object add(HttpServletRequest request, String json) {
        logService.log(111,json);
        JSONObject resObj=new JSONObject();
        JSONObject jsonObject=JSONObject.parseObject(json);
        TPAdata data=JSONObject.parseObject(jsonObject.getString("Data"),TPAdata.class);
        data.setGuid(jsonObject.getString("Guid"));
        data.setFileName(jsonObject.getString("FileName"));
        if(save(data)){
            if(data.getPercentageEigenvalueDatas()!=null) {
                for (TPAPEdata item : data.getPercentageEigenvalueDatas()) {
                    item.setAnalysisID(data.getId());
                }
                boolean bl= tpaPEdataService.saveBatch(data.getPercentageEigenvalueDatas());
                if(!bl){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚
                    resObj.put("Result",false);
                    resObj.put("Message","数据保存失败");
                    return resObj;
                }
            }

            if(data.getFrequencyDistributionDatas()!=null) {
                for (TPAFDdata item : data.getFrequencyDistributionDatas()) {
                    item.setAnalysisID(data.getId());
                }
                boolean bl= tpaFDdataService.saveBatch(data.getFrequencyDistributionDatas());
                if(!bl){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚
                    resObj.put("Result",false);
                    resObj.put("Message","数据保存失败");
                    return resObj;
                }
            }
        }
        resObj.put("Result",true);
        resObj.put("Message","上传成功");
        return resObj;
    }

    @Override
    public Object get(HttpServletRequest request, String bh, String ph) {
        QueryWrapper<TPAdata> qw=new QueryWrapper<>();
        if(bh!=null){
            qw.eq("SampleSerialNumber",bh);
        }
        if (ph!=null){
            qw.eq("SampleLotNo",ph);
        }
        List<TPAdata> list=list(qw);
        if(list.isEmpty()){
            list=new ArrayList<>();
            qw=new QueryWrapper<>();
            qw.orderByDesc("id");
            qw.last("limit 1");
            TPAdata tpa=getOne(qw);
            if(tpa!=null) {
                list.add(getOne(qw));
            }
        }
        for (TPAdata item : list) {
            QueryWrapper<TPAPEdata> qw1=new QueryWrapper<>();
            qw1.eq("AnalysisID",item.getId());
            item.setPercentageEigenvalueDatas(tpaPEdataService.list(qw1));
            QueryWrapper<TPAFDdata> qw2=new QueryWrapper<>();
            qw2.eq("AnalysisID",item.getId());
            item.setFrequencyDistributionDatas(tpaFDdataService.list(qw2));
        }
        return JsonResult.success(list);
    }
}
